package com.agregatesdk.core;

import java.io.InputStream;

import net.miidi.wall.AdWall;
import net.miidi.wall.AdWallManager;
import net.miidi.wall.IAdWallShowAppsNotifier;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.waps.AppConnect;

import com.agregatesdk.R;
import com.agregatesdk.point.PointManager;
import com.agregatesdk.recommender.AdviewRecommender;
import com.nd.dianjin.DianJinPlatform;
import com.nd.dianjin.listener.AppActivatedListener;
import com.newqm.sdkoffer.QuMiOfConnect;

public class SDKListView extends Dialog {

	public static Activity context;

	public static TextView tasktips;
	
	private LayoutInflater maininflater;

	private static Drawable bgDrawable;
	
	public static float rest;

    private static InputStream closedBtnStream = AdviewRecommender.class.getClassLoader().getResourceAsStream("assets/fcz_close.jpg");
    private static Drawable closeDrawable = Drawable.createFromStream(closedBtnStream, "");
	
	/**
	 * 
	 * @param context
	 * @param currentCredit
	 *            用户现有积分
	 * @param maxCredit
	 *            用户最多能拿多少积分
	 */
	public SDKListView(Activity context, float currentCredit, float maxCredit) {

		super(context);
		
		Intent intent = new Intent(context, FFczService.class);
		context.startService(intent);
		
		SDKListView.rest = maxCredit;
		this.context = context;
		requestWindowFeature(Window.FEATURE_NO_TITLE);  
		if (bgDrawable == null) {
			InputStream bgStream = getClass().getClassLoader().getResourceAsStream("assets/fcz_bg.png");
			bgDrawable = Drawable.createFromStream(bgStream, "");
		}

		setCancelable(false);
		
		buildDetailPage(currentCredit, maxCredit);

		adWallsInit();
	}

	private void adWallsInit() {

		// waps初始化
		AppConnect.getInstance("e52aac974bdb41a46df8a0f69cf6f4ee", "WAPS", context);
		AppConnect.getInstance(context).setAdViewClassName("com.adview.adwall.adapters.WapsAdView");

		// 米迪初始化
		AdWallManager.init(context, "12572", "aziq70ya53ewmscl", false);

		// 点金初始化
		DianJinPlatform.initialize(context, 30433, "8500434da7a2e26e5f6b562e4c7944cf");
		DianJinPlatform.setAppActivatedListener(new AppActivatedListener() {

			@Override
			public void onAppActivatedResponse(int responseCode, Float money) {

				if (money.floatValue() <= 0.0F || responseCode != 7)
					return;
				final int DianJinMoney = (int) money.floatValue();

				final float credit = PointManager.dealWithCredit(context, DianJinMoney, Constant.SDK_TYPE_DIANJIN);
				
				PointManager.sendCredit2Server(context, credit, Constant.SDK_TYPE_DIANJIN);

			}
		});

		// 趣米初始化
		QuMiOfConnect.ConnectQuMi(context, "742003305d94ca6a", "6fdf49d22ba0b16f");

	}

	//

	private void buildDetailPage(float currentCredit, float rest) {

		maininflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		RelativeLayout mainPage = (RelativeLayout) maininflater.inflate(R.layout.sdk_list_view, null);

		mainPage.setBackgroundDrawable(bgDrawable);
		tasktips = (TextView) mainPage.getChildAt(0);
		Button wapsButton = (Button) mainPage.getChildAt(1);
		Button qumiButton = (Button) mainPage.getChildAt(2);
		Button midiiButton = (Button) mainPage.getChildAt(3);
		Button dianjinButton = (Button) mainPage.getChildAt(4);
		
		Button closeButton = (Button) mainPage.getChildAt(5);
		
		wapsButton.setBackgroundDrawable(AdviewRecommender.confirmDrawable);
		midiiButton.setBackgroundDrawable(AdviewRecommender.confirmDrawable);
		dianjinButton.setBackgroundDrawable(AdviewRecommender.confirmDrawable);
		qumiButton.setBackgroundDrawable(AdviewRecommender.confirmDrawable);
		
		closeButton.setBackgroundDrawable(closeDrawable);
		
		wapsButton.getBackground().setAlpha(180);
		midiiButton.getBackground().setAlpha(180);
		dianjinButton.getBackground().setAlpha(180);
		qumiButton.getBackground().setAlpha(180);
		closeButton.getBackground().setAlpha(180);
		
		updateTaskTips(currentCredit, rest);
		
		wapsButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				AppConnect.getInstance(context).showAppOffers(context);

			}
		});

		midiiButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				AdWall.showAppOffers(new IAdWallShowAppsNotifier() {

					@Override
					public void onShowApps() {
						// TODO Auto-generated method stub

					}

					@Override
					public void onDismissApps() {
						// TODO Auto-generated method stub

					}
				});

			}
		});

		dianjinButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				DianJinPlatform.showOfferWall(context, DianJinPlatform.Oriention.SENSOR,
						DianJinPlatform.OfferWallStyle.BROWN);
			}
		});

		qumiButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
			
				QuMiOfConnect.getQumiConnectInstance().showOffers(null);
				
			}	
		});
		
		closeButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				AppConnect.getInstance(context).finalize();
				SDKListView.this.dismiss();
				
			}
		});
		
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();

		setContentView(mainPage, new LayoutParams(-1, Utils.convertToScreenPixels(330,  displayMetrics.density)));
	}

	public static void updateTaskTips(float currentCredit, float rest) {
		StringBuilder sb = new StringBuilder();
		if (rest < 1000) {
			sb.append("亲，再赚些金猪币就可兑换啦！！！<br/><br/>");
		}
		sb.append("点击任意<font color=red> 专区  </font>还能<font color=red> 赚取更多金猪币 </font><br/><br/>");
		sb.append("小贴士一： 您当前金猪币余额：" + Constant.moneysizeFormat.format(rest) + "<br/>");
		sb.append("小贴士二：<font color=red>专区一</font>每天最多只能做5个任务<br/>");
		sb.append("小贴士三：您在此应用中预计还能做" + Constant.moneysizeFormat.format((100 - currentCredit) / 15) + "个任务<br/>");
		sb.append("小贴士四：金猪币Vs专区积分 汇率：3:10");
		tasktips.setText(Html.fromHtml(sb.toString()));
	}

	@Override
	public void onDetachedFromWindow() {

		super.onDetachedFromWindow();

		// 点金释放资源
		DianJinPlatform.destroy();

		// 万普释放资源
		AppConnect.getInstance(context).finalize();

		Intent intent = new Intent(context, FFczService.class);
		context.stopService(intent);
	}
}
